package com.example.papercutting.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.papercutting.R;

public class ColorActivity extends AppCompatActivity {
    private View selectedShape; // 展示选中形状的视图
    private String selectedPaperType; // 记录纸张类型
    private int selectedColor = Color.RED; // 记录选择的颜色

    private ColorBarView mCbv_color_pick;
//    private TextView mTv_text_color_a;
//    private TextView mTv_text_color_b;
//    private TextView mTv_text_color_c;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color);

        //        initView();

        // 接收从上一页面传递的参数
        Intent intent = getIntent();
        selectedPaperType = intent.getStringExtra("selectedPaperType");

        // 初始化视图
        selectedShape = findViewById(R.id.selected_shape);
        TextView shapeLabel = findViewById(R.id.shape_label);

        ImageButton leftArrow = findViewById(R.id.left_arrow);
        ImageButton rightArrow = findViewById(R.id.right_arrow);

        // 根据传递的纸张类型动态设置形状和文字
        switch (selectedPaperType) {
            case "square":
                selectedShape.setBackground(getResources().getDrawable(R.drawable.square_shape));
                shapeLabel.setText("正方形纸张");
                break;
            case "octagon":
                selectedShape.setBackground(getResources().getDrawable(R.drawable.diamond_shape));
                shapeLabel.setText("八边形纸张");
                break;
            case "circle":
            default:
                selectedShape.setBackground(getResources().getDrawable(R.drawable.circle_shape));
                shapeLabel.setText("圆形纸张");
                break;
        }

        mCbv_color_pick = findViewById(R.id.cbv_color_pick);
//        mTv_text_color_a = findViewById(R.id.tv_text_color_a);
//        mTv_text_color_b = findViewById(R.id.tv_text_color_b);
//        mTv_text_color_c = findViewById(R.id.tv_text_color_c);
//        mTv_text_color_a.setBackgroundColor(-7601921);
//        mTv_text_color_c.setBackgroundColor(-16711891);

        //        initListener();
//        mTv_text_color_a.setOnClickListener(this::onClick);
//        mTv_text_color_b.setOnClickListener(this::onClick);
//        mTv_text_color_c.setOnClickListener(this::onClick);
        mCbv_color_pick.setOnColorChangerListener(new ColorBarView.OnColorChangeListener() {
            @Override
            public void onColorChange(int color) {
                Log.i("COLOR_INT", "颜色发生变化 " + color);
//                mTv_text_color_b.setBackgroundColor(color);
                changeShapeColor(color);
                selectedColor = color;
            }
        });
        mCbv_color_pick.setCurrentColor(-16711726);

        // 左箭头点击事件（返回上一个界面）
        leftArrow.setOnClickListener(v -> {
            Intent backIntent = new Intent(ColorActivity.this, SelectionActivity.class);
            startActivity(backIntent);
        });

        // 右箭头点击事件（跳转到下一步）
        rightArrow.setOnClickListener(v -> {
            Toast.makeText(ColorActivity.this, "当前选中颜色：" + selectedColor, Toast.LENGTH_SHORT).show();
            // 这里可以传递选择的颜色参数到下一个界面
            Intent nextIntent = new Intent(ColorActivity.this, PartActivity.class);
            nextIntent.putExtra("selectedPaperType", selectedPaperType);
            nextIntent.putExtra("selectedColor", selectedColor);
            startActivity(nextIntent);
        });
    }


    private void initView() {

    }

    private void initListener() {

    }

//    public void onClick(View view) {
//        if (view.getId() == R.id.tv_text_color_a)
//            mCbv_color_pick.setCurrentColor(((ColorDrawable) mTv_text_color_a.getBackground()).getColor());
//        else if (view.getId() == R.id.tv_text_color_b)
//            mCbv_color_pick.setCurrentColor(((ColorDrawable) mTv_text_color_b.getBackground()).getColor());
//        else
//            mCbv_color_pick.setCurrentColor(((ColorDrawable) mTv_text_color_c.getBackground()).getColor());
//    }

    // 改变形状颜色：仅填充颜色，保持形状不变
    private void changeShapeColor(int color) {
        // 获取当前背景 Drawable
        Drawable backgroundDrawable = selectedShape.getBackground();

        // 检查背景是否为 Drawable，并应用颜色过滤器
        if (backgroundDrawable != null) {
            // 设置颜色过滤器，保持形状不变
            backgroundDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        }
    }
}
